package main

import (
	"illuminant/config"
	"illuminant/logger"

	"github.com/gin-gonic/gin"
	"github.com/urfave/cli/v2"
)

func ServerCmd() *cli.Command {

	return &cli.Command{
		Name:    "server",
		Aliases: []string{"s"},
		Flags: []cli.Flag{
			&cli.BoolFlag{
				Name:  "prod",
				Usage: "production mode",
			},
			&cli.StringFlag{
				Name:  "f",
				Usage: "setting file path",
				Value: "settings.toml",
			},
		},
		Action: func(c *cli.Context) error {
			err := config.InitConfig(c.String("f"))
			if err != nil {
				return err
			}

			cnf := config.GetConfig()
			// init logger
			logger.InitLogger(cnf.Logger.Level, cnf.Logger.Dir, cnf.Logger.Filename)

			// is prod mode
			if c.Bool("prod") {
				gin.SetMode(gin.ReleaseMode)
			} else {
				gin.SetMode(gin.DebugMode)
			}

			// start route
			return route(cnf.Server.Port)
		},
	}
}
